<template>
  <zm-dialog
    :title="pTitle"
    :visible.sync="value"
    :before-close="dialogClose"
    append-to-body
    :close-on-click-modal="false"
    size="small"
  >
    <el-form ref="form" :model="form" label-width="140px" :rules="Rules">
      <el-form-item label="客户名称" prop="name">
        <zm-input
          v-model="form.name"
          maxlength="50"
          placeholder="请输入客户名称,建议不超过50个字"
          class="w350"
        ></zm-input>
      </el-form-item>
      <el-form-item label="所属行业" prop="tradeType">
        <zm-data-select
          v-model="form.tradeType"
          k1="tradeType"
          class="w350"
        ></zm-data-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="tr">
      <zm-button @click="dialogClose" type="default">返回</zm-button>
      <zm-button @click="submit" type="primary">保存</zm-button>
    </div>
  </zm-dialog>
</template>

<script>
import dialogMixin from '@/mixins/dialogMixin'
import { updateType, createType } from '@/api/src/customerSet'
export default {
  name: '',
  components: {},
  mixins: [dialogMixin],
  props: {

  },
  data () {
    return {
      form: {
        name: '',
        tradeType: ''
      },

      Rules: {
        name: { required: true, message: '请填写名称', trigger: 'blur' },
        tradeType: { required: true, message: '请选择所属行业', trigger: 'change' }
      }
    }
  },
  computed: {
    pTitle () {
      const method = this.obj.id ? '编辑客户' : '新增客户'
      return method
    }
  },
  created () {

  },

  methods: {
    async submit () {
      let _valid = false
      this.$refs.form.validate(valid => {
        _valid = valid
      })
      if (!_valid) return
      const _api = this.obj.id ? updateType : createType
      await _api({
        type: '6',
        id: this.obj.id,
        name: this.form.name,
        tradeType: this.form.tradeType
      })
      await this.$store.dispatch('setTypeList', 'forceFresh')
      this.$message.success('操作成功')
      this.$emit('after')
      this.dialogClose()
    }
  }
}
</script>

<style scoped lang="stylus"></style>
